//
//  UIView+CQPopupBottomPanlineAction.m
//  AppCommonUICollect
//
//  Created by ciyouzen on 2019/8/13.
//  Copyright © 2019 dvlproad. All rights reserved.
//

#import "UIView+CQPopupBottomPanlineAction.h"
#import <Masonry/Masonry.h>
#import <CJPopupContainer/CQPopupInfo+Effect.h>
#import <CQPopupContainerAnimation/UIView+CJPopupFrameAnimation.h>


@implementation UIView (CQPopupBottomPanlineAction)

#pragma mark - 从底部弹出当前视图的相关代码
#pragma mark runtime
// 真正执行popup操作的视图 realPopupView
- (UIView *)cqCustom_realPopupView {
    UIView *realPopupView = self.cjPopupInfo.popupView;
    return realPopupView;
}


#pragma mark Event
/*
 *  显示当前视图到window底部(可自定制点击空白区域执行的是确认还是取消操作)
 *
 *  @param popupSuperview               弹出视图的父视图view(如果为nil，则会弹出到window上)
 *  @param selfHeight                   本视图被干净弹出的高度
            （即使本视图最终是由其他视图包着弹出,这里也只输入本视图被干净弹出的高度,实际最终的弹窗高度,内部会自动计算后以最终高度弹出)
 *  @param cornerType                   圆角类型
 *  @param effectType                   模糊类型
 *  @param shouldEnableTapBlank         是否允许点击空白区域（常设为YES，来实现点击空白区域来隐藏弹窗的功能）
 *  @param tapBlankViewCompleteBlock    当允许点击空白区域时候的执行事件(为nil时，点击会自动隐藏；非nil时候需要自己调用隐藏)
 *  @param shouldAddPanAction           是否添加仿抖音评论的下拉拖动手势(附对于那些会弹出键盘的视图，一般设为NO，即不添加)
 *  @param panCompleteDismissBlock      拖动结束需要执行dimiss的回调(shouldAddPanAction为NO的时候，此值无效，相当于设为nil)
 */
- (void)cqPopup_bottom_with_panline_showInView:(nullable UIView *)popupSuperview
                                withHeight:(CGFloat)selfHeight
                                cornerType:(CQBottomPopupViewCornerRadiusFor)cornerType
                                effectType:(CQEffectFor)effectType
                      shouldEnableTapBlank:(BOOL)shouldEnableTapBlank
                          tapBlankComplete:(void(^ _Nullable)(void))tapBlankViewCompleteBlock
                        shouldAddPanAction:(BOOL)shouldAddPanAction
                   panCompleteDismissBlock:(void(^ _Nullable)(void))panCompleteDismissBlock
{
    // 执行显示弹窗的方法
    CQBottomPopupViewEdgeInsetsType edgeInsetsType = CQBottomPopupViewEdgeInsetsTypeDefault;
    CQPopupInfo *popupInfo = [CQPopupInfo addBottom_panline_inView:popupSuperview clearView:self clearViewHeight:selfHeight edgeInsetsType:edgeInsetsType tapBlankComplete:^{
        // 设置点击空白区域的方法
        if (shouldEnableTapBlank == NO) {
            return;
        }
        
        if (tapBlankViewCompleteBlock) {
            tapBlankViewCompleteBlock();
        } else {
            [self cqPopup_bottom_with_panline_hide];
        }
        
    } shouldAddPanAction:shouldAddPanAction panCompleteDismissBlock:^{
        if (panCompleteDismissBlock) {
            panCompleteDismissBlock();
        } else {
            [self cqPopup_bottom_with_panline_hide];
        }
    }];
    
    
    // 修饰弹窗
    [CQPopupInfo configPopupInfo:popupInfo withCornerType:cornerType effectType:effectType];
    
    
    // 弹出弹窗
    [self cj_popupWithViewInfo:popupInfo isToBottom:YES];
}

/**
 *  从window底部隐藏当前视图
 */
- (void)cqPopup_bottom_with_panline_hide {
    [self cj_hidePopupView];
}


#pragma mark - Update(比较少用)

/*
 *  更新视图的高度（常使用带有输入文本框的弹出视图，随着输入内容的长度变化，高度会变化）
 *
 *  @param viewHeightWithoutPanLine                   除去顶部下拉线之外的其他视图的高度
 （即使本视图最终是由其他视图包着弹出,这里也只输入本视图被干净弹出的高度,实际最终的弹窗高度,内部会自动计算后以最终高度弹出)
 */
- (void)cqPopup_bottom_with_panline_updateViewWithoutPanLineHeight:(CGFloat)viewHeightWithoutPanLine {
    UIView *realPopupView = self.cqCustom_realPopupView;
    
    CQPopupInfo *popupInfo = (CQPopupInfo *)self.cjPopupInfo;
    CGFloat panLineAndSapcingHeight = popupInfo.panLineAndSapcingHeight;
    CGFloat realPopupViewHeight = panLineAndSapcingHeight + viewHeightWithoutPanLine;
    
    [UIView cqCustom_panline_updateRealPopupView:realPopupView realPopupViewHeight:realPopupViewHeight];
    
    // TODO:为什么已经是主线程，还必须放主线程才会更新到
    dispatch_async(dispatch_get_main_queue(), ^{
        [self mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@(viewHeightWithoutPanLine));
        }];
    });
}

/*
 *  更新视图的高度（常使用带有输入文本框的弹出视图，随着输入内容的长度变化，高度会变化）
 *
 *  @param selfHeight                   本视图被干净弹出的高度
 （即使本视图最终是由其他视图包着弹出,这里也只输入本视图被干净弹出的高度,实际最终的弹窗高度,内部会自动计算后以最终高度弹出)
 */
+ (void)cqCustom_panline_updateRealPopupView:(UIView *)realPopupView realPopupViewHeight:(CGFloat)realPopupViewHeight {
    CGFloat oldMaxY = CGRectGetMaxY(realPopupView.frame);

    CGRect oldFrame = realPopupView.frame;
    oldFrame.size.height = realPopupViewHeight;
    oldFrame.origin.y = oldMaxY-realPopupViewHeight; // 记得操作此步，上移y
    realPopupView.frame = oldFrame;
}


@end
